package com.chenjun.designpattern.structural.decorator;

/**
 * 首先举个例子，JDK的IO类里面 Reader fr = new FileReader(); BufferedReader buff = new
 * BufferedReader(fr); 就是一个装饰器的例子
 * 
 * @author admin
 *
 */
public class 装饰器模式
{

	public static void main(String[] args)
	{
		Shape circle = new Circle();
		Shape readCircle = new RedShapeDecorator(new Circle());
		
		circle.draw();
		readCircle.draw();
		
		System.out.println("---------------------------------");
		
		Shape rectangle = new Rectangle();
		Shape redRectangle = new RedShapeDecorator(rectangle);
		rectangle.draw();
		redRectangle.draw();
	}

}

/**
 * 创建一个接口。
 * @author admin
 *
 */
interface Shape
{
	void draw();
}

/**
 * 创建实现接口的实体类。
 * @author admin
 *
 */
class Rectangle implements Shape
{

	@Override
	public void draw()
	{
		System.out.println("Shape: Rectangle");
	}
}

/**
 * 创建实现接口的实体类。
 * @author admin
 *
 */
class Circle implements Shape
{

	@Override
	public void draw()
	{
		System.out.println("Shape: Circle");
	}
}

/**
 * 接口的抽象装饰器类
 * @author admin
 *
 */
abstract class ShapeDecorator implements Shape
{
	protected Shape decoratedShape;

	public ShapeDecorator(Shape decoratedShape)
	{
		this.decoratedShape = decoratedShape;
	}

	public void draw()
	{
		decoratedShape.draw();
	}
}

/**
 * 接口装饰器类的实现子类,
 * @author admin
 *
 */
class RedShapeDecorator extends ShapeDecorator
{

	public RedShapeDecorator(Shape decoratedShape)
	{
		super(decoratedShape);
	}

	@Override
	public void draw()
	{
		decoratedShape.draw();
		setRedBorder(decoratedShape);
	}

	private void setRedBorder(Shape decoratedShape)
	{
		System.out.println("该图形填充了红色");
	}
}
